<?
include($classes_path.'/class.poll.database.php');
include($classes_path.'/class.poll.php');

$db = new database;

# Set query to see if voted already
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM ".$prefix."`votes` WHERE `vote_ip` = '" . $ip . "'";

$poll = new poll;

# If a match is found show results only
if($db->num_rows($sql) == 1)
{
    # Show results
    print($poll->showResults());
}
else
{
    # Check if form submitted
    if(!$_POST['vote'])
    {
        # Show form
        print($poll->showOptions());
    }
    else
    {
        # Add data to database
        # Check for a value
        if(!$_POST['poll'])
        {
            # No option selected
            # Show poll again
            print($poll->showOptions());
        }
        else
        {
            # A radio is checked
            # Update votes
            print($poll->updateVotes($_POST['poll']));

            # Show results
            print($poll->showResults());
        }
    }
}
?> 